package com.chb.friend1错误的combiner;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Reducer;
public class FofReducer extends Reducer<Text, Text, Text, Text>{
	 
	@Override
	protected void reduce(Text key, Iterable<Text> values, Context context)
			throws IOException, InterruptedException {
		Map<Integer, String> maps = new HashMap<Integer, String>();
		String a = key.toString();
		for (Text value : values) {
			String b = value.toString().split("\t")[0];
			int i = Integer.parseInt(value.toString().split("\t")[1]);
			if (maps.get(i) == null) {
				maps.put(i, b+":"+i);
			}else {
				String tmp = maps.get(i);
				maps.put(i, tmp+";"+b+":"+i);
			}
		}
	    Set<Integer> keys = maps.keySet();
	    List<Integer> as = new ArrayList<Integer>();
		for (Integer integer : keys) {
			as.add(integer);
		}
		Collections.sort(as);
		String result = null;
		for (int i = 0; i < as.size(); i++) {
			result += as.get(i);
		}
		context.write(key, new Text(result));
		
		
	}
}
